
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */


#include "main.h"
#include "adc.h"
#include "rtc.h"
#include "usart.h"
#include "gpio.h"




#include <stdio.h> 
#include <string.h>
#include "BC26.h"
#include "TelecomPlatform.h"
#include "Network_communication.h"
#include "MHL10.h" 
#include "flash.h"
#include "M120L.h"






#define VERSION "LoRa-NB-5in1-V1.6-2021-11-24-360-12h-168h"


unsigned char UART1_Rx_Buf[MAX_REC_LENGTH] = {0};                
unsigned int  UART1_Rx_cnt = 0;                 
unsigned char UART1_temp[REC_LENGTH] = {0};



uint8_t Wireless_Mode = LoRaWAN_Mode; 
eDeviceMode gDeviceRunMode = Water_Dip_Mode;

uint16_t	Fire_Hydrant_Range  = 1600; 
uint16_t	Water_Pressure_Range = 2000; 
uint16_t	Water_Level_Range    = 5000; 

uint16_t Ref_Val;
uint16_t Battary_Val;
uint16_t adc_calt_value;

uint8_t Sent_Data[66]={'2','4','2','4','0','0','8','8'};
uint8_t MHL10_TxData[50];
uint8_t Sample_Data[8];
uint8_t M120L_TxData[50];

uint8_t	Fire_Hydrant_Flag  = 0; 
uint8_t	Water_Pressure_Flag = 0; 
uint8_t	Water_Level_Flag    = 0; 

uint16_t MeasureTimes = 0x00;
uint8_t SentTimes = 0x00;


uint16_t PeriodSent_MeasureTimes = 144;  
uint16_t Reboot_MeasureTimes = 2016; 



uint8_t Period_Sent_Flag = 0;
uint8_t NIC_Sent_Flag = 0;
uint8_t Alarm_Sent_Flag = 0;


uint8_t INT_DISMOUNT_IO = 0x00;
uint8_t INT_TRIG_IO = 0x00;
uint8_t INT_USER_IO2 = 0x00;
uint8_t INT_USER_IO1 = 0x00;

uint8_t DISMOUNT_IO = '0';
uint8_t TRIG_IO = '0';
uint8_t USER_IO2 = '0';
uint8_t USER_IO1 = '0';
uint8_t Sample_Val_Alarm = '0';

int8_t	SocketId=-1;
uint8_t	Send_err_time=0;
uint8_t 	SendMessageFlag=0;
uint8_t	  Bc26_Initial_Flag=FALSE;
uint8_t Bc26_CESQ;

uint8_t LoRa_Detect = 0; 
uint8_t LoRa_Config = 0; 

uint8_t LoRaWAN_Detect = 0; 

uint8_t Rece_len;
uint8_t Recebuffer[ReceiveUdpDataMaxLen];

uint8_t Simcard_exist = 0x00;















/**********************  version introduce begin ******************

SW1.3: NB power off time is 1.5s. lora lorawan nb upload and download data success,
			 test is ok. but an-xin-gong two water-level device do not upload data when 
			 system reboot later.

SW1.4: NB power off delay time change to 5s, wakeup bc26 before system reboot,
       bc26-module's cap uncharge,3.6V fall to below 1.5V
			 
SW1.5: if simcard is not detect,bc26 1.8V maybe not fall to 0V,so bc26 will not 
				goto deepsleep success,but goto deepsleep cmd is success. It will read 
				simcard iccid when period system reboo,so id iccid fail,poweroff bc26.
				reboot,

***********************  version introduce end ********************/



void SystemClock_Config(void);







void LED_blink(uint8_t time)
{
	while(time--)
	{
		LED1_ON;
		HAL_Delay(100);
		LED1_OFF;
		HAL_Delay(100);
	}
}

void Nb_Data_Communication(void)
{
	uint8_t  Result=FALSE;
	uint8_t i;
	if(SocketId<0)
	{
		SocketId=BC26_IntialAndCreatSocket();
	}
	else
	{	
		SysPrintf(">>wakeup bc26\r\n");
		BC26_EINT3V3_H;
		HAL_Delay(30);
		BC26_EINT3V3_L;
		HAL_Delay(1000);
		
	}
	if(SocketId>=0)
	{
		Bc26_CESQ=BC26_Check_CESQ();
		
		Result=SendMessage_To_Server();
		
		
		if(Result==TRUE)
		{
			LED_blink(5);
			SysPrintf("sent data success and wait cmd\r\n");
			Send_err_time = 0;			
      
			Receive_Message_From_Server();
			if(Rece_len>0)
			{
				SysPrintf("receive nb cmd len=%d\n",Rece_len);
				SysPrintf("receive nb cmd=%s\n",Recebuffer);
				LED_blink(3);
				CMDProcess(Recebuffer,Rece_len);
				Rece_len = 0;
				for(i=0;i<ReceiveUdpDataMaxLen;i++)
					Recebuffer[i] = 0;
			}
								    	
		}
		else
		{
			LED_blink(1);
			SysPrintf("sent data fail and try again\r\n");
			Send_err_time++;
			Result=SendMessage_To_Server(); 
			if(Result==TRUE)
			{
				LED_blink(5);
				SysPrintf("sent data success and wait cmd\r\n");
				Send_err_time = 0;				    
				
				Receive_Message_From_Server();
				if(Rece_len>0)
				{
					SysPrintf("receive nb cmd len=%d\n",Rece_len);
					SysPrintf("receive nb cmd=%s\n",Recebuffer);
					LED_blink(3);
					CMDProcess(Recebuffer,Rece_len);
					Rece_len = 0;
					for(i=0;i<ReceiveUdpDataMaxLen;i++)
					Recebuffer[i] = 0;
				}
			}
			else
			{
				LED_blink(1);
				Send_err_time++;
				SysPrintf("sent data fail again\r\n");
				
			}

			if(Send_err_time>=4)
			{
				Send_err_time=0;
				SocketId=-1;	
				SysPrintf("SocketId=-1\n");
			}
		}
	}
	else
	{
		SysPrintf("nb initial and creatsocket err\r\n");
		Send_err_time=0;
		SocketId=-1;	
		SysPrintf("SocketId=-1\r\n");			
	}
	Bc26_Initial_Flag=BC26_ConfigSleepMode(BC26_EnableSleepMode);
	/*
	if(Simcard_exist==0x00)
	{
		SysPrintf("not found simcard and poweroff bc26\r\n");		
		NB_PW_OFF;
		SocketId=-1;
		Send_err_time=0;
	}
	*/
}



void Lora_Data_Communication(void)
{
	uint8_t i;
	uint8_t j = 0;
	uint8_t Sent_ret = 0;
	MX_USART1_UART_Init();
	HAL_UART_Receive_IT(&huart1,(uint8_t *)UART1_temp,REC_LENGTH);
	
	for(i=0;i<8;i++)  
		MHL10_TxData[i] = Sent_Data[i];
	for(i=43;i<48;i++)  
		MHL10_TxData[i-27] = Sent_Data[i];
	
	Sample_Data[1] = Battary_Val;
	Sample_Data[0] = Battary_Val>>8;
	Sample_Data[3] = adc_calt_value;
	Sample_Data[2] = adc_calt_value>>8;
	Sample_Data[4] = 0x00;
	Sample_Data[5] = SentTimes;
	Sample_Data[6] = 0x00; 
	Sample_Data[7] = 0xAA; 
	
	for(i=0;i<8;i++)
	{
		sprintf(MHL10_TxData+21+j*2,"%02x",Sample_Data[i]);
		++j;
	}
	MHL10_TxData[37] = 0;
	
	

	SysPrintf("LoRa sent data:%s\r\n",MHL10_TxData);
	
	if(Send_err_time>=4)
	{
		Send_err_time=0;
		MHL10_Initial_Config();	
	}
	
	
	Sent_ret = MHL10_Sent_Packet_To_Gateway(MHL10_TxData,37);
	if(Sent_ret==2) 
	{
		LED_blink(5);	
		Send_err_time = 0;
		if(Rece_len>0)
		{
			SysPrintf("receive nb cmd len=%d\n",Rece_len);
			SysPrintf("receive nb cmd=%s\n",Recebuffer);
			LED_blink(3);
			CMDProcess(Recebuffer,Rece_len);
			Rece_len = 0;
			for(i=0;i<ReceiveUdpDataMaxLen;i++)
				Recebuffer[i] = 0;
		}
	}
	else if(Sent_ret==1) 
	{
		LED_blink(5);
		; 
		Send_err_time = 0;
	}
	else if(Sent_ret==0)
	{
		LED_blink(1);	
		Send_err_time++;
		SysPrintf("sent data fail and try again\r\n");
		Sent_ret = MHL10_Sent_Packet_To_Gateway(MHL10_TxData,37);
		if(Sent_ret==2)
		{
			LED_blink(5);
			Send_err_time = 0;
			if(Rece_len>0)
			{
				SysPrintf("receive lora cmd len=%d\n",Rece_len);
				SysPrintf("receive lora cmd=%s\n",Recebuffer);
				LED_blink(3);
				CMDProcess(Recebuffer,Rece_len);
				Rece_len = 0;
				for(i=0;i<ReceiveUdpDataMaxLen;i++)
					Recebuffer[i] = 0;
			}
		}
		else if(Sent_ret==1)
		{
			LED_blink(5);
			; 
			Send_err_time = 0;
		}
		else if(Sent_ret==0)
		{
			LED_blink(1);
			Send_err_time++;
			SysPrintf("sent data fail again\r\n");
		}
	}

		
	
	
	
	
    LED0_OFF;		
	SendMessageFlag=0;
	
	
		
		
		
	
	
	
}

void LoraWAN_Data_Communication(void)
{
	uint8_t i;
	uint8_t j = 0;
	int8_t LoRaWAN_JOIN_Status;
	int8_t LoRaWAN_Sent_Status;
	int8_t LoRaWAN_Sleep_ret;
	for(i=0;i<8;i++)  
		M120L_TxData[i] = Sent_Data[i];
	for(i=43;i<48;i++)  
		M120L_TxData[i-35] = Sent_Data[i];
	
	Sample_Data[1] = Battary_Val;
	Sample_Data[0] = Battary_Val>>8;
	Sample_Data[3] = adc_calt_value;
	Sample_Data[2] = adc_calt_value>>8;
	Sample_Data[4] = 0x00;
	Sample_Data[5] = SentTimes;
	Sample_Data[6] = 0x00; 
	Sample_Data[7] = 0xAA; 
	
	for(i=0;i<8;i++)
	{
		sprintf(M120L_TxData+13+j*2,"%02x",Sample_Data[i]);
		++j;
	}
	M120L_TxData[29] = 0;

	SysPrintf("LoRaWAN sent data:%s\r\n",M120L_TxData);
	
	M120_Wakeup_INT();
	
	if(Send_err_time>=4)
	{
		Send_err_time=0;
		LoRaWAN_Detect = 0;
		M120_Initial();	
	}
	LoRaWAN_JOIN_Status =  Get_M120_Status_INT();
	if(LoRaWAN_JOIN_Status == -1) 
	{
		M120_Join_Net_INT();
		LoRaWAN_JOIN_Status =  Get_M120_Status_INT();
		if(LoRaWAN_JOIN_Status==-1) 
		{
			Send_err_time++;
		}
		else 
		{
			LoRaWAN_Sent_Status = M120_TxData_INT(M120L_TxData, 29);
			if(LoRaWAN_Sent_Status==-1)
			{
				Send_err_time++;
				LED1_ON;
				HAL_Delay(100);
				LED1_OFF;		
			}
			else
			{
				Send_err_time = 0;
				M120_RecvData_INT();
				if(Rece_len>0)
				{
					SysPrintf("receive lorawan cmd len=%d\n",Rece_len);
					SysPrintf("receive lorawan cmd=%s\n",Recebuffer);
					LED_blink(3);
					CMDProcess(Recebuffer,Rece_len);
					Rece_len = 0;
					for(i=0;i<ReceiveUdpDataMaxLen;i++)
						Recebuffer[i] = 0;
				}
			}
		}
	}
	else if(LoRaWAN_JOIN_Status == 1) 
	{
		LoRaWAN_Sent_Status = M120_TxData_INT(M120L_TxData, 29);
		if(LoRaWAN_Sent_Status==-1)
		{
			Send_err_time++;
			LED1_ON;
			HAL_Delay(100);
			LED1_OFF;
		}
		else
		{
			Send_err_time = 0;
			M120_RecvData_INT();
			if(Rece_len>0)
			{
				SysPrintf("receive lorawan cmd len=%d\n",Rece_len);
				SysPrintf("receive lorawan cmd=%s\n",Recebuffer);
				LED_blink(3);
				CMDProcess(Recebuffer,Rece_len);
				Rece_len = 0;
				for(i=0;i<ReceiveUdpDataMaxLen;i++)
					Recebuffer[i] = 0;
			}
		}
	}
	else
	{
		;
	}
	
	LoRaWAN_Sleep_ret = M120_sleep01_INT();
	if(LoRaWAN_Sleep_ret==-1)
	{
		LoRaWAN_Sleep_ret = M120_sleep01_INT();
		if(LoRaWAN_Sleep_ret==-1)
		{
			NB_PW_OFF;
			LoRaWAN_Detect = 0;
		}
	}

}



/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  

		

		uint16_t adc_vref_value;
		uint16_t Fire_Hydrant_Low_th;
		uint16_t Fire_Hydrant_High_th;
		uint16_t Water_Pressure_Low_th;
		uint16_t Water_Pressure_High_th;
		uint16_t Water_Level_Low_th;
		uint16_t Water_Level_High_th;

		MeasureTimes = 0;
	
  

  

  
  HAL_Init();

  

		__HAL_RCC_PWR_CLK_ENABLE();
	HAL_PWREx_EnableUltraLowPower();
	HAL_PWREx_EnableFastWakeUp();
	__HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_HSI);	
	
  

  
  SystemClock_Config();

  

  

  
  MX_GPIO_Init();
  MX_RTC_Init();
  MX_USART2_UART_Init();
  MX_ADC_Init();
  MX_USART1_UART_Init();
  
	LED0_ON;
	NB_PW_OFF;
	SysPrintf("%s\r\n",VERSION);
	HAL_Delay(1500);
	
	if(0==Read_Config())
	{
		Wireless_Mode = LoRaWAN_Mode;   
		gDeviceRunMode = Water_Pressure_Mode;  
	}
	
	Fire_Hydrant_Low_th = Fire_Hydrant_Range*0.05;
	Fire_Hydrant_High_th = Fire_Hydrant_Range*0.1;
	Water_Pressure_Low_th = Water_Pressure_Range*0.05;
	Water_Pressure_High_th = Water_Pressure_Range*0.1;	
	Water_Level_Low_th = Water_Level_Range*0.1;
	Water_Level_High_th = Water_Level_Range*0.2;
	
	HAL_UART_Receive_IT(&huart1,(uint8_t *)UART1_temp,REC_LENGTH); 
	
	
	
	if(gDeviceRunMode==Fire_Hydrant_Mode)
	{
		SysPrintf("device:Fire_Hydrant_Mode and range=%d\r\n",Fire_Hydrant_Range);
	}
	else if(gDeviceRunMode==Water_Pressure_Mode)
	{
		SysPrintf("device:Water_Pressure_Mode and range=%d\r\n",Water_Pressure_Range);
	}
	else if(gDeviceRunMode==Water_Level_Mode)
	{
		SysPrintf("device:Water_Level_Mode and range=%d\r\n",Water_Level_Range);
	}
	else if(gDeviceRunMode==Door_Sensor_Mode)
	{
		SysPrintf("device:Door_Sensor_Mode\r\n");
	}
	else if(gDeviceRunMode==Water_Dip_Mode)
	{
		SysPrintf("device:Water_Dip_Mode\r\n");
	}
	else
	{
		SysPrintf("device-type:error\r\n");
	}
	
	
	if(Wireless_Mode==Nb_Mode)
	{
		SysPrintf("Commu-type:NB-IoT\r\n");
		Sent_Data[6] = '3';
		Sent_Data[7] = '8';
		SocketId=BC26_IntialAndCreatSocket();
	}
	else if(Wireless_Mode==Lora_Mode)
	{
		Sent_Data[6] = '1';
		Sent_Data[7] = 'D';
		SysPrintf("Commu-type:LoRa\r\n");
		MHL10_Initial_Config();
		;
	}
	else if(Wireless_Mode==LoRaWAN_Mode)
	{
		Sent_Data[6] = '1';
		Sent_Data[7] = '5'; 
		SysPrintf("Commu-type:LoRaWAN\r\n");
		M120_Initial();
		Get_M120L_Info();
		;
	}
	else
	{
		SysPrintf("Commu-type:error\r\n");
	}
	
	Sent_Data[4] = Wireless_Mode + '0';
		
	while(1)
	{
		LED0_ON;
		
		SysPrintf(">>%d\r\n",MeasureTimes);
		Sent_Data[43] = '0';
		Sent_Data[44] = '0';
		Sent_Data[45] = '0';
		Sent_Data[46] = '0';
		Sent_Data[47] = '0';
		HAL_UART_Receive_IT(&huart1,(uint8_t *)UART1_temp,REC_LENGTH); 
		
		
		if(Get_Dismount_PIN_STATUS==1)
			Sent_Data[43] = '1';
		else
			Sent_Data[43] = '0';
		
		if(gps14a_PIN_STATUS==0)
			Sent_Data[44] = '1';
		else
			Sent_Data[44] = '0';
		
		if(door_sensor2_PIN_STATUS==1)
			Sent_Data[45] = '1';
		else
			Sent_Data[45] = '0';
		
		if(door_sensor1_PIN_STATUS==1)
			Sent_Data[46] = '1';
		else
			Sent_Data[46] = '0';
		
		
		
		if(INT_DISMOUNT_IO==1)
		{
			HAL_Delay(15);
			if(Get_Dismount_PIN_STATUS==1)
			{
				DISMOUNT_IO = '1';
				SysPrintf("INT_DISMOUNT_IO=high\r\n");
			}
			else
			{
				DISMOUNT_IO = '0';
				SysPrintf("INT_DISMOUNT_IO=low\r\n");
			}
			NIC_Sent_Flag = 1;
			Sent_Data[43] = DISMOUNT_IO;
		}
		if(INT_TRIG_IO==1)
		{
			SysPrintf("INT_TRIG_IO\r\n");
			HAL_Delay(15);
			if(gps14a_PIN_STATUS==0)
			{
				TRIG_IO = '1';
			}
			else
			{
				TRIG_IO = '0';
			}
			NIC_Sent_Flag = 1;
			Sent_Data[44] = TRIG_IO;
		}
		
		if(INT_USER_IO2==1)
		{
			
			HAL_Delay(15);
			if(door_sensor2_PIN_STATUS==1)
			{
				USER_IO2 = '1';
				SysPrintf("INT_USER_IO2=high\r\n");
			}
			else
			{
				USER_IO2 = '0';
				SysPrintf("INT_USER_IO2=low\r\n");
			}
			NIC_Sent_Flag = 1;
			Sent_Data[45] = USER_IO2;
		}
		
		if(INT_USER_IO1==1)
		{
			
			HAL_Delay(15);
			if(door_sensor1_PIN_STATUS==1)
			{
				USER_IO1 = '1';
				SysPrintf("INT_USER_IO1=high\r\n");
			}
			else
			{
				USER_IO1 = '0';
				SysPrintf("INT_USER_IO1=low\r\n");
			}
			NIC_Sent_Flag = 1;
			Sent_Data[46] = USER_IO1;
		}
		
		
		
		if(MeasureTimes%PeriodSent_MeasureTimes==0)
		{
			SysPrintf(">>period sample batval\r\n");
			Period_Sent_Flag = 1;
			adc_vref_value = ADC_Vref_Data();
			Battary_Val = 	3000*(VREFINT_CAL_H<<8|VREFINT_CAL_L)/adc_vref_value;
		}
		
		
		
		
		
		if(gDeviceRunMode==Fire_Hydrant_Mode)
		{
			Sent_Data[5] = '1';
			SysPrintf("device:Fire_Hydrant_Mode\r\n");
			adc_calt_value = ADC_channel0_Data();
			adc_calt_value = adc_calt_value*Battary_Val/2048; 
			SysPrintf("sample-pressval=%d\r\n",adc_calt_value);
			if( (adc_calt_value<=500) &&(adc_calt_value>=50))
			{
				adc_calt_value = 500;
				
				adc_calt_value = 1;
			}
			else if(adc_calt_value<50)
			{
				
				adc_calt_value = 0;
			}
			else
			{				
				adc_calt_value = Fire_Hydrant_Range*(adc_calt_value-500)/4000;
			}
			SysPrintf("Ref_Val=%d,Batval=%d,pressval=%d\r\n",adc_vref_value,Battary_Val,adc_calt_value);
			
			if(MeasureTimes==0) 
			{
				if(adc_calt_value<=Fire_Hydrant_Low_th) 
					Fire_Hydrant_Flag = 1;
				else
					Fire_Hydrant_Flag = 0;
			}
			
			if( (adc_calt_value<=Fire_Hydrant_Low_th)&&(Fire_Hydrant_Flag==0) ) 
			{
				Fire_Hydrant_Flag = 1;
				Alarm_Sent_Flag  = 1;
				Sample_Val_Alarm = '1';
			}
			else if((adc_calt_value>=Fire_Hydrant_High_th)&&(Fire_Hydrant_Flag==1))
			{
				Fire_Hydrant_Flag = 0;
				Alarm_Sent_Flag = 1;
				Sample_Val_Alarm = '0';
			}
			else
			{
				;
			}
		}
		else if(gDeviceRunMode==Water_Pressure_Mode)
		{
			Sent_Data[5] = '2';
			SysPrintf("device:Water_Pressure_Mode\r\n");
			adc_calt_value = ADC_channel0_Data();
			adc_calt_value = adc_calt_value*Battary_Val/2048; 
			SysPrintf("sample-pressval=%d\r\n",adc_calt_value);
			if( (adc_calt_value<=500) &&(adc_calt_value>=50))
			{
				adc_calt_value = 500;
				
				adc_calt_value = 1;
			}
			else if(adc_calt_value<50)
			{
				
				adc_calt_value = 0;
			}
			else
			{				
				adc_calt_value = Water_Pressure_Range*(adc_calt_value-500)/4000;
			}
			SysPrintf("Ref_Val=%d,Batval=%d,pressval=%d\r\n",adc_vref_value,Battary_Val,adc_calt_value);
			
			if(MeasureTimes==0) 
			{
				if(adc_calt_value<=Water_Pressure_Low_th) 
					Water_Pressure_Flag = 1;
				else
					Water_Pressure_Flag = 0;
			}
			
			if( (adc_calt_value<=Water_Pressure_Low_th)&&(Water_Pressure_Flag==0) ) 
			{
				Water_Pressure_Flag = 1;
				Alarm_Sent_Flag  = 1;
				Sample_Val_Alarm = '1';
			}
			else if((adc_calt_value>=Water_Pressure_High_th)&&(Water_Pressure_Flag==1))
			{
				Water_Pressure_Flag = 0;
				Alarm_Sent_Flag = 1;
				Sample_Val_Alarm = '0';
			}
			else
			{
				;
			}
		}
		
		else if(gDeviceRunMode==Water_Level_Mode)
		{
			Sent_Data[5] = '3';
			SysPrintf("device:Water_Level_Mode\r\n");
			adc_calt_value = ADC_channel1_Data();
			adc_calt_value = adc_calt_value*Battary_Val/4096;
			SysPrintf("sample-waterlevel=%d\r\n",adc_calt_value);
			if( (adc_calt_value<=400) &&(adc_calt_value>=40))
			{
				adc_calt_value = 400;
				
				adc_calt_value = 1;
			}
			else if(adc_calt_value<40)
			{
				
				adc_calt_value = 0;
			}
			else
			{				
				adc_calt_value = Water_Level_Range*(adc_calt_value-400)/1600;
			}
			SysPrintf("Ref_Val=%d,Batval=%d,waterlevel=%d\r\n",adc_vref_value,Battary_Val,adc_calt_value);
			
			if(MeasureTimes==0) 
			{
				if(adc_calt_value<=Water_Level_Low_th) 
					Water_Level_Flag = 1;
				else
					Water_Level_Flag = 0;
			}

			if( (adc_calt_value<=Water_Level_Low_th)&&(Water_Level_Flag==0) ) 
			{
				Water_Level_Flag = 1;
				Alarm_Sent_Flag  = 1;
				Sample_Val_Alarm = '1';
			}
			else if((adc_calt_value>=Water_Level_High_th)&&(Water_Level_Flag==1))
			{
				Water_Level_Flag = 0;
				Alarm_Sent_Flag = 1;
				Sample_Val_Alarm = '0';
			}
			else
			{
				;
			}
			
		}
		
		else if(gDeviceRunMode==Door_Sensor_Mode) 
		{
			SysPrintf("device:Door_Sensor_Mode\r\n");
			Sent_Data[5] = '4';
		}
		
		else if(gDeviceRunMode==Water_Dip_Mode) 
		{
			SysPrintf("device:Water_Dip_Mode\r\n");
			Sent_Data[5] = '5';
		}
		
		else
		{
			;
		}
		
		
		
		if(Period_Sent_Flag==1)
		{
			if((gDeviceRunMode==Door_Sensor_Mode) || (gDeviceRunMode==Water_Dip_Mode))
			{
				adc_vref_value = ADC_Vref_Data();
				Battary_Val = 	3000*(VREFINT_CAL_H<<8|VREFINT_CAL_L)/adc_vref_value;
				
			}
			SentTimes++;
			Sent_Data[47] = '0';
			SysPrintf("period sent data\r\n");
		}
		else if(NIC_Sent_Flag==1)
		{
			if((gDeviceRunMode==Door_Sensor_Mode) || (gDeviceRunMode==Water_Dip_Mode))
			{
				adc_vref_value = ADC_Vref_Data();
				Battary_Val = 	3000*(VREFINT_CAL_H<<8|VREFINT_CAL_L)/adc_vref_value;
				adc_calt_value = 0;
			}
			Sent_Data[47] = '1';
			SentTimes++;
			SysPrintf("NVIC sent data\r\n");
		}
		else if(Alarm_Sent_Flag==1)
		{
			SysPrintf("alarm sent data\r\n");
			Sent_Data[47] = '2';
			SentTimes++;
			;
		}
		
		else
		{
			;
		}
		
		
		
		if( (NIC_Sent_Flag==1)||(Alarm_Sent_Flag==1)||(Period_Sent_Flag==1) )
		{
			if(Wireless_Mode==Nb_Mode)
			{
				SysPrintf("NB-IoT sent data\r\n");
				Nb_Data_Communication();
				if(Bc26_Initial_Flag==FALSE)
				{
					Bc26_Initial_Flag=BC26_PowerStartAndSleep();
					SocketId=-1;
				}				
			 }
			else if(Wireless_Mode==Lora_Mode)   
			{					
				SysPrintf("LoRa sent data\r\n");
				if(LoRa_Detect==1)
				{
					Lora_Data_Communication();
				}
				else
				{
					if(Check_MHL10()==FALSE)
					{
						SysPrintf("LoRa detect error\r\n");
						LoRa_Detect = 0;
					}
					else
					{
						LoRa_Detect = 1;
						Lora_Data_Communication();
					}

				}					
			}
			
			else if(Wireless_Mode==LoRaWAN_Mode)   
			{					
				SysPrintf("LoRaWAN sent data\r\n");
				if(LoRaWAN_Detect==1)
				{
					LoraWAN_Data_Communication();
				}
				else
				{
					M120_Initial();
					if(LoRaWAN_Detect==1)
					{
						LoraWAN_Data_Communication();
					}
					else
					{
						SysPrintf("LoRa detect error\r\n");
						LoRaWAN_Detect = 0;
					}
				}					
			}
			
			else
			{
				;
			}
												 
		}
		
		
		
		
		DC_DC2_OFF;
		DC_DC1_OFF;
		DC_5_0V_OFF;
		
		INT_DISMOUNT_IO = 0x00;
		INT_TRIG_IO = 0x00;
		INT_USER_IO2 = 0x00;
		INT_USER_IO1 = 0x00;
		NIC_Sent_Flag = 0;
		Alarm_Sent_Flag = 0;
		Period_Sent_Flag = 0;
		
		MeasureTimes++;
		if(MeasureTimes%Reboot_MeasureTimes==0) 
		{
			SocketId=-1;
			
			if(Wireless_Mode==Nb_Mode)
			{
				SysPrintf(">>wakeup bc26\r\n");
				BC26_EINT3V3_H;
				HAL_Delay(30);
				BC26_EINT3V3_L;
				HAL_Delay(30);
				
			}
			else if(Wireless_Mode==LoRaWAN_Mode)
			{
				SysPrintf(">>wakeup M120L\r\n");
				M120_Wakeup_INT();
			}
			else if(Wireless_Mode==Lora_Mode)
			{
				SysPrintf(">>wakeup MHL10\r\n");
				
				Wakeup_MHL10_HW();
				
				
				;
			}
			else
			{
				;
			}
			SysPrintf(">>system reboot>>>\r\n");
			HAL_NVIC_SystemReset();
		}
		LED1_OFF;
		LED0_OFF;
		DC_DC2_OFF;
		DC_DC1_OFF;
		DC_5_0V_OFF;
		
		
		
		if( (Simcard_exist==0x00) && (Wireless_Mode==Nb_Mode) ) 
		{
			SysPrintf("not found simcard and poweroff bc26\r\n");		
			NB_PW_OFF;
			SocketId=-1;
			Send_err_time=0;
		}
		
		
		
		SysPrintf("go to sleep\r\n");
		HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);		
	}

  

  
  
  while (1)
  {
    

    
  }
  
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2
                              |RCC_PERIPHCLK_RTC;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}





/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  
  
  __disable_irq();
  while (1)
  {
  }
  
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  
  /* User can add his own implementation to report the file name and line number,
     ex: SysPrintf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  
}
#endif 


